<template>
  <div class="card content-box">
    <el-form>
      <el-form-item label="驻留集个数 :">
        <el-input v-model="formData.num" disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="内存存取时间：">
        <el-input v-model="formData.time" disabled="disabled" size="default" />
      </el-form-item>
      <el-form-item label="缺页中断时间：">
        <el-input v-model="formData.pause" disabled="disabled" />
      </el-form-item>
      <el-form-item label="是否有快表？ :">
        <el-switch v-model="formData.form" disabled="disabled" />
      </el-form-item>
      <el-form-item label="快表时间：" v-if="formData.form">
        <el-input v-model="formData.fastform" disabled="disabled" />
      </el-form-item>
      <el-form-item label="快表和页表是否同时访问？ :" v-if="formData.form">
        <el-switch v-model="formData.together" disabled="disabled" />
      </el-form-item>
      <el-form-item label="逻辑地址访问序列：">
        <el-input v-model="formData.data" disabled="disabled" />
      </el-form-item>
      <div class="demo-progress">
        <el-progress :percentage="percentageResult" :stroke-width="25" striped striped-flow :duration="100" />
      </div>
      <div class="table-container">
        <div class="table-group">
          <div>
            <table class="custom-table" border="2">
              <caption>
                FIFO
              </caption>
              <tr v-for="(row, rowIndex) in data1.twoDArray" :key="rowIndex">
                <td class="cellSet" v-for="(value, colIndex) in row" :key="colIndex">
                  <div v-if="value !== -1">
                    {{ value }}
                  </div>
                </td>
              </tr>
            </table>
          </div>
          <div>
            <table class="custom-table" border="2">
              <caption>
                LRU
              </caption>
              <tr v-for="(row, rowIndex) in data1.twoDArray1" :key="rowIndex">
                <td v-for="(value, colIndex) in row" :key="colIndex">
                  <div v-if="value !== -1">
                    {{ value }}
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>

        <div class="table-group">
          <div>
            <table class="custom-table" border="2">
              <caption>
                LFU
              </caption>
              <tr v-for="(row, rowIndex) in data1.twoDArray2" :key="rowIndex">
                <td v-for="(value, colIndex) in row" :key="colIndex">
                  <div v-if="value !== -1">
                    {{ value }}
                  </div>
                </td>
              </tr>
            </table>
          </div>
          <div>
            <table class="custom-table" border="2">
              <caption>
                OPT
              </caption>
              <tr v-for="(row, rowIndex) in data1.twoDArray3" :key="rowIndex">
                <td class="cellSet" v-for="(value, colIndex) in row" :key="colIndex">
                  <div v-if="value !== -1">
                    {{ value }}
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
      <el-form-item style="margin-left: 750px">
        <el-button type="primary" @click="onStop" size="large">暂停</el-button>
        <el-button type="primary" @click="onStart" size="large">继续</el-button>
        <el-button type="primary" v-if="data.onSave" :icon="Download" @click="methods.save()" size="large" />
      </el-form-item>
      <div style="margin: 50px"></div>
      <div ref="echartsContainer" style="height: 300px; width: 100%"></div>
      <div ref="echartsContainer2" style="height: 300px; width: 100%"></div>
    </el-form>
  </div>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import { useFormDataStore } from "@/stores/data/formData";
import * as echarts from "echarts";
import { Download } from "@element-plus/icons-vue";

const echartsContainer = ref(null);
const echartsContainer2 = ref(null);
let percentageCount = ref(0);
let rows = ref();
let cols = ref();
let FromDataStore = useFormDataStore();
const formData = ref({
  data: "",
  form: false,
  fastform: "",
  num: 1,
  time: "",
  pause: "",
  count: 0,
  address1: "",
  address2: "",
  together: false,
  auto: false,
  chartView: false
});
let data1 = ref({});
let percentageResult = ref({});
onMounted(() => {
  // 初始化
  data.onSave = false;
  formData.value = FromDataStore.getFormData;
  data.pageCount = formData.value.num;
  data.strs = formData.value.data;
  data.strsArray = data.strs.split(" ");
  rows.value = formData.value.num + 4;
  cols.value = data.strsArray.length + 1;
  data1 = ref({
    twoDArray: new Array(rows.value).fill(-1).map(() => new Array(cols.value).fill(-1)),
    twoDArray1: new Array(rows.value).fill(-1).map(() => new Array(cols.value).fill(-1)),
    twoDArray2: new Array(rows.value).fill(-1).map(() => new Array(cols.value).fill(-1)),
    twoDArray3: new Array(rows.value).fill(-1).map(() => new Array(cols.value).fill(-1)),
    pageFault: 0,
    pageFaultLRU: 0,
    pageFaultLFU: 0,
    pageFaultOPT: 0,
    pageFaultRate: [],
    pageFaultRateLRU: [],
    pageFaultRateLFU: [],
    pageFaultRateOPT: []
  });
  percentageResult.value = parseFloat(((percentageCount.value * 100) / (data.strsArray.length * 4)).toFixed(2));
  dataLFU.position = 0;
  myChart = echarts.init(echartsContainer.value);
  myChart2 = echarts.init(echartsContainer2.value);
  option = {
    title: {
      text: "页面置换算法比较"
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    grid: {
      right: "10%" // Adjust the value based on your needs
    },
    legend: {
      data: ["FIFO", "LRU", "LFU", "OPT"]
    },
    xAxis: {
      type: "category",
      data: data.strsArray,
      name: "逻辑地址序列"
    },
    yAxis: {
      type: "value",
      name: "页面置换次数"
    },
    series: [
      {
        data: data1.value.shortPageCount,
        type: "line",
        name: "FIFO" // Add a name for the series
      },
      {
        data: data1.value.shortPageCountLRU,
        type: "line",
        name: "LRU" // Add a name for the series
      },
      {
        data: data1.value.shortPageCountLFU,
        type: "line",
        name: "LFU" // Add a name for the series
      },
      {
        data: data1.value.shortPageCountOPT,
        type: "line",
        name: "OPT" // Add a name for the series
      }
    ]
  };
  option2 = {
    title: {
      text: "页面置换算法缺页率"
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    legend: {
      data: ["FIFO", "LRU", "LFU", "OPT"]
    },
    xAxis: [
      {
        type: "category",
        data: data.strsArray,
        axisTick: {
          alignWithLabel: true
        },
        name: "逻辑地址序列"
      }
    ],
    yAxis: [
      {
        type: "value",
        name: "缺页率"
      }
    ],
    series: [
      {
        name: "FIFO",
        type: "bar",
        barWidth: "10%",
        data: data1.value.pageFaultRate
      },
      {
        name: "LRU",
        type: "bar",
        barWidth: "10%",
        data: data1.value.pageFaultRateLRU
      },
      {
        name: "LFU",
        type: "bar",
        barWidth: "10%",
        data: data1.value.pageFaultRateLFU
      },
      {
        name: "OPT",
        type: "bar",
        barWidth: "10%",
        data: data1.value.pageFaultRateOPT
      }
    ]
  };
  methods.runAsyncMethods();
  result.algorithm.pageCount = formData.value.num;
  result.algorithm.memoryTime = formData.value.time;
  result.algorithm.pageFaultTime = formData.value.pause;
  result.algorithm.fastTablePd = formData.value.form;
  result.algorithm.togetherPd = formData.value.together;
  data.onSave = true;
});
const data = reactive({
  //FIFO算法处理单个逻辑地址所花费的时间
  onSave: false,
  shortPageCount: [],
  shortPageCountLRU: [],
  shortPageCountLFU: [],
  shortPageCountOPT: [],
  timeFIFO: 0,
  time: 0,
  fastTime: 0,
  pause: 0,
  // 逻辑地址序列
  stop: false,
  logicalAddresses: [],
  pageTable: null,
  fastTable: null,
  memory: [],
  memoryLRU: [],
  /**
   * 快表页表模式：
   * 1：无快表
   * 2：查询快表与页表同时进行
   * 3：查询快表与页表交替进行
   */
  fastTablePd: formData.value.form,
  fastPagePd: formData.value.together,
  //FIFO页面置换算法
  //驻留内存页面最大个数
  pageCount: formData.value.num,
  //页面队列
  pageQueue: [],
  //字符串组：存储以空格分隔的逻辑地址序列
  strs: formData.value.data,
  shortPage: 0,
  shortPageLRU: 0,
  // LRU算法中所用到的变量
  // 页表(使用哈希表存储)
  pageTableLRU: null,
  // 存储页号的访问循序
  pageQueueLRU: [],
  // 快表(使用哈希表存储)
  fastTableLRU: null,
  // LRU页面置换算法情况下，处理一个逻辑地址所耗费的时间
  timeLRU: 0
  // memorySequences: [] // 存储每次生成的 data.memory 数据

  // 创建一个 10*10 的二维数组，并初始化为0
});
const result = {
  algorithm: {
    pageCount: 0,
    memoryTime: 0,
    pageFaultTime: 0,
    addressLength: 0,
    fastTablePd: false,
    togetherPd: false
  },
  algorithmFIFO: {
    name: "FIFO",
    pageFaultRate: 0,
    physicalAddress: [],
    pageFault: 0,
    time: []
  },
  algorithmLRU: {
    name: "LRU",
    pageFaultRate: 0,
    physicalAddress: [],
    pageFault: 0,
    time: []
  },
  algorithmLFU: {
    name: "LFU",
    pageFaultRate: 0,
    physicalAddress: [],
    pageFault: 0,
    time: []
  },
  algorithmOPT: {
    name: "OPT",
    pageFaultRate: 0,
    physicalAddress: [],
    pageFault: 0,
    time: []
  }
};
const dataLFU = reactive({
  // 字符串序列
  strs: formData.value.data,
  //逻辑地址序列
  logicalAddresses: [],
  // 存储处理每个逻辑地址的时间
  time: [],
  // 存储单个逻辑地址的物理地址
  singleTime: 0,
  // 内存存取时间
  memoryTime: formData.value.time,
  // 缺页中断时间
  pageFaultTime: formData.value.pause,
  // 访问快表时间
  fastAccessTime: formData.value.fastform,
  //内存物理块数
  pageCount: formData.value.num,
  // 统计是否缺页
  pageFault: [],
  //统计是否缺页
  singlePageFault: false,
  // 缺页中断次数
  pageFaultCount: 0,
  // 模拟内存
  memory: [],
  // 是否使用快表判断
  useFastPd: formData.value.form,
  // 快表与页表是否一起被访问的判断
  useTogetherPd: formData.value.together,
  // 模拟页表
  pageTable: {
    // 将页号与物理块号一一对应
    entries: [],
    // 存储页面状态（即页面是否在内存）
    state: [],
    pageNumber: 0
  },
  // 模拟位置
  position: 0,
  //模拟快表
  fastTable: {
    // 将页号与物理块号一一对应
    entries: [],
    // 存储页面状态（即页面是否在内存）
    state: [],
    pageNumber: 0
  },
  // 记录每个页面的访问次数
  accessCount: [],
  //缺页率
  pageFaultRate: 0
});
const dataOPT = reactive({
  // 字符串序列
  strs: formData.value.data,
  //逻辑地址序列
  logicalAddresses: [],
  // 存储处理每个逻辑地址的时间
  time: [],
  // 存储单个逻辑地址的物理地址
  singleTime: 0,
  // 内存存取时间
  memoryTime: formData.value.time,
  // 缺页中断时间
  pageFaultTime: formData.value.pause,
  // 访问快表时间
  fastAccessTime: formData.value.fastform,
  //内存物理块数
  pageCount: formData.value.num,
  // 统计是否缺页
  pageFault: [],
  //统计是否缺页
  singlePageFault: false,
  // 缺页中断次数
  pageFaultCount: 0,
  // 模拟内存
  memory: [],
  // 是否使用快表判断
  useFastPd: formData.value.form,
  // 快表与页表是否一起被访问的判断
  useTogetherPd: formData.value.together,
  // 模拟页表
  pageTable: {
    entries: [],
    state: []
  },
  //模拟快表
  fastTable: {
    entries: [],
    state: []
  },
  // 存储未来将要访问的页号队列
  pageQueue: [],
  pageQueueLength: 0,
  // 存储未来将要访问的地址的偏移地址队列
  offsetQueue: [],
  // 存储未来将要访问的逻辑地址的偏移队列
  logicOffsetQueue: [],
  // 存储每个页面下一次出现的索引
  nextIndex: [],
  // 索引变量
  index: 0,
  // 位置
  position: 0
});
const dataLRU = reactive({
  singleTime: 0,
  time: [],
  fastAccessTime: 0,
  memoryTime: 0,
  pageFaultTime: 0,
  fastTablePd: false,
  useTogetherPd: false
});
const onStop = () => {
  data.stop = true;
};
const onStart = () => {
  data.stop = false;
};
let myChart = null;
let myChart2 = null;
let option = null;
let option2 = null;

const updateChart = () => {
  option.xAxis.data = data.strsArray;
  option.series[0].data = data.shortPageCount;
  option.series[1].data = data.shortPageCountLRU;
  option.series[2].data = data.shortPageCountLFU;
  option.series[3].data = data.shortPageCountOPT;
  myChart.setOption(option, true);
};
const updataChart2 = () => {
  option2.xAxis.data = data.strsArray;
  option2.series[0].data = data1.value.pageFaultRate;
  option2.series[1].data = data1.value.pageFaultRateLRU;
  option2.series[2].data = data1.value.pageFaultRateLFU;
  option2.series[3].data = data1.value.pageFaultRateOPT;
  myChart2.setOption(option2, true);
};

const methods = {
  sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  },
  strsToStr() {
    //处理字符串组strs
    //trim()方法用于删除字符串的前后空格
    data.strs = data.strs.trim();
    //split()方法用于将一个字符串分割成字符串数组
    const strArray = data.strs.split(" ");
    data.logicalAddresses = strArray;
  },
  strsToStrLRU() {
    //处理字符串组strs
    //trim()方法用于删除字符串的前后空格
    data.strs = data.strs.trim();
    //split()方法用于将一个字符串分割成字符串数组
    const strArrayLRU = data.strs.split(" ");
    data.logicalAddressesLRU = strArrayLRU;
  },
  strsToStrLFU() {
    dataLFU.strs = dataLFU.strs.trim();
    const strArray = dataLFU.strs.split(" ");
    dataLFU.logicalAddresses = strArray;
    console.log(dataLFU.logicalAddresses + "dataLFU");
  },
  strsToStrOPT() {
    dataOPT.strs = dataOPT.strs.trim();
    const strArray = dataOPT.strs.split(" ");
    dataOPT.logicalAddresses = strArray;
  },
  dealWithFIFO(pageNumber) {
    if (!data.fastTablePd) {
      //没有快表
      let pageState = data.pageTable.state[pageNumber];
      if (pageState) {
        data.shortPage = 0;
        //页面已经在内存中
        data.timeFIFO = data.time * 2;
        return data.pageTable.entries[pageNumber];
      } else {
        data.shortPage = 1;
        //页面不在内存中
        //检查内存是否已满
        data.timeFIFO = data.time * 2 + data.pause;
        if (data.pageQueue.length < data.pageCount) {
          //内存未满，直接将页面加入内存和队列
          //memoryNumber:内存块号
          let memoryNumber = data.pageQueue.length;
          data.pageTable.entries[pageNumber] = memoryNumber;
          data.pageTable.state[pageNumber] = true;
          data.memory[memoryNumber] = pageNumber;
          data.pageQueue.push(pageNumber);
          return memoryNumber;
        } else {
          //内存已满，采用FIFO算法进行页面置换
          //将页面队列中的第一个页面置换出 shift()方法：删除队列的第一个元素，并返回该元素的值
          let replacedPageNumber = data.pageQueue.shift();
          //被替换的物理帧号
          let replacedFrameNumber = data.pageTable.entries[replacedPageNumber];
          //将新的页面加入内存和队列
          data.pageTable.entries[pageNumber] = replacedFrameNumber;
          data.pageTable.state[pageNumber] = true;
          data.memory[replacedFrameNumber] = pageNumber;
          data.pageQueue.push(pageNumber);
          //更改页表中被替换页面的状态为false
          data.pageTable.state[replacedPageNumber] = false;
          return replacedFrameNumber;
        }
      }
    } else if (data.fastTablePd) {
      //访问快表和页表同时进行
      let quickPageState = data.fastTable.state[pageNumber];
      if (quickPageState) {
        data.timeFIFO = data.fastTime + data.time;
        return data.quickTable.entries[pageNumber];
      } else {
        let pageTableState = data.pageTable.state[pageNumber];
        if (pageTableState) {
          // 页表中存在该页面的信息
          //将信息送入快表
          data.fastTable.entries[pageNumber] = data.pageTable.entries[pageNumber];
          data.fastTable.state[pageNumber] = true;
          if (formData.value.together) {
            data.timeFIFO = data.time * 2;
          } else {
            data.timeFIFO = data.time * 2 + data.fastTime;
          }
          return data.pageTable.entries[pageNumber];
        } else {
          //页面不在内存
          if (formData.value.together) {
            data.timeFIFO = data.time * 2 + data.pause;
          } else {
            data.timeFIFO = data.time * 2 + data.pause + data.fastTime;
          }
          //检查内存是否已满
          if (data.pageQueue.length < data.pageCount) {
            //内存未满，直接将页面加入内存和队列
            //memoryNumber:内存块号
            let memoryNumber = data.pageQueue.length;
            data.pageTable.entries[pageNumber] = memoryNumber;
            data.pageTable.state[pageNumber] = true;
            //将新增页面信息送入快表
            data.fastTable.entries[pageNumber] = memoryNumber;
            data.fastTable.state[pageNumber] = true;
            data.memory[memoryNumber] = pageNumber;
            data.pageQueue.push(pageNumber);
            return memoryNumber;
          } else {
            //内存已满，采用FIFO算法进行页面置换
            //将页面队列中的第一个页面置换出 shift()方法：删除队列的第一个元素，并返回该元素的值
            let replacedPageNumber = data.pageQueue.shift();
            //被替换的物理帧号
            let replacedFrameNumber = data.pageTable.entries[replacedPageNumber];
            //将新的页面加入内存和队列
            data.pageTable.entries[pageNumber] = replacedFrameNumber;
            data.pageTable.state[pageNumber] = true;
            //将新增页面信息送入快表
            data.fastTable.entries[pageNumber] = replacedFrameNumber;
            data.fastTable.state[pageNumber] = true;
            data.memory[replacedFrameNumber] = pageNumber;
            //更改页表和快表中被替换页面的状态为false
            data.pageTable.state[replacedPageNumber] = false;
            data.fastTable.state[replacedPageNumber] = false;
            data.pageQueue.push(pageNumber);
          }
        }
      }
    }
  },
  dealWithLRU(pageNumber) {
    console.log("dealWithLRU");
    //判断当前是否有快表
    if (!data.fastTablePd) {
      // 检查页表中是否存在该页
      if (data.pageTableLRU.has(pageNumber)) {
        data.shortPageLRU = 0;
        dataLRU.singleTime = dataLRU.memoryTime * 2;
        // 页面已在页表中，更新访问顺序
        methods.updateState(pageNumber);
        return data.pageTableLRU.get(pageNumber);
      } else {
        data.shortPageLRU = 1;
        // 页面不在页表中，需要进行页面置换
        //  检查内存中是否还有空闲位置
        dataLRU.singleTime = dataLRU.memoryTime * 2 + dataLRU.pageFaultTime;
        if (data.pageQueueLRU.length < data.pageCount) {
          // 内存有空闲位置，直接将页面调入内存
          // 将新页面加入页表和访问顺序
          data.memoryLRU[data.pageQueueLRU.length] = pageNumber;
          data.pageTableLRU.set(pageNumber, data.pageQueueLRU.length);
          data.pageQueueLRU.push(pageNumber);
          //返回该页面对应的物理块号
          return data.pageQueueLRU.length - 1;
        } else {
          // 内存没有空闲位置，需要进行页面置换
          // 选择最近最久未使用的页面进行置换
          // shift方法的作用
          // 用于删除数组的第一个元素，并返回该元素。
          const victimPageNumber = data.pageQueueLRU.shift();
          // 获取被置换页面页号所对应的物理块号
          const victimPhysicalBlockNumber = data.pageTableLRU.get(victimPageNumber);
          data.pageTableLRU.delete(victimPageNumber);
          // 将新页面加入页表和访问顺序
          data.pageTableLRU.set(pageNumber, victimPhysicalBlockNumber);
          data.pageQueueLRU.push(pageNumber);
          data.memoryLRU[victimPhysicalBlockNumber] = pageNumber;
          return victimPhysicalBlockNumber;
        }
      }
    } else {
      // 检查快表中是否存在该页
      if (data.fastTableLRU.has(pageNumber)) {
        // 页面已在快表中，更新访问顺序
        methods.updateState(pageNumber);
        dataLRU.singleTime = dataLRU.fastAccessTime + dataLRU.memoryTime;
        return data.fastTableLRU.get(pageNumber);
      } else {
        // 页面不在快表中
        // 检查页表中是否存在该页
        if (data.pageTableLRU.has(pageNumber)) {
          // 页面在页表中，更新访问顺序
          methods.updateState(pageNumber);
          if (dataLRU.useTogetherPd) {
            dataLRU.singleTime = dataLRU.memoryTime + dataLRU.memoryTime;
          } else {
            dataLRU.singleTime = dataLRU.memoryTime * 2 + dataLRU.fastAccessTime;
          }
          return data.pageTableLRU.get(pageNumber);
        } else {
          // 页面不在页表中
          if (dataLRU.useTogetherPd) {
            dataLRU.singleTime = dataLRU.memoryTime * 2 + dataLRU.pageFaultTime;
          } else {
            dataLRU.singleTime = dataLRU.memoryTime * 2 + dataLRU.fastAccessTime + dataLRU.pageFaultTime;
          }
          // 检查内存中是否存在空闲位置
          if (data.pageQueueLRU.length < data.pageCount) {
            // 内存中存在空闲位置
            // 将新页面加入页表、快表和访问顺序
            data.pageTableLRU.set(pageNumber, data.pageQueueLRU.length);
            data.fastTableLRU.set(pageNumber, data.pageQueueLRU.length);
            data.memoryLRU[data.pageQueueLRU.length] = pageNumber;
            data.pageQueueLRU.push(pageNumber);
            return data.pageQueueLRU.length - 1;
          } else {
            //内存中不存在空闲位置
            // 选择最近最少使用的页面进行替换
            let victimPageNumber = data.pageQueueLRU.shift();
            let victimPhysicalBlockNumber = data.pageTableLRU.get(victimPageNumber);
            data.pageTableLRU.delete(victimPageNumber);
            data.fastTableLRU.delete(victimPageNumber);
            data.pageTableLRU.set(pageNumber, victimPhysicalBlockNumber);
            data.fastTableLRU.set(pageNumber, victimPhysicalBlockNumber);
            data.pageQueueLRU.push(pageNumber);
            data.memoryLRU[victimPhysicalBlockNumber] = pageNumber;
            return victimPhysicalBlockNumber;
          }
        }
      }
    }
  },
  dealWithLFU(pageNumber) {
    // 不使用快表的情况下
    if (!dataLFU.useFastPd) {
      // 如果页面已在内存中
      if (dataLFU.pageTable.state[pageNumber]) {
        dataLFU.singleTime = dataLFU.memoryTime * 2;
        dataLFU.singlePageFault = false;
        dataLFU.accessCount[pageNumber]++;
        return dataLFU.pageTable.entries[pageNumber];
      } else {
        // 表示缺页
        dataLFU.singlePageFault = true;
        //页面不在内存中,发生缺页中断
        dataLFU.pageFaultCount++;
        dataLFU.accessCount[pageNumber] = 1;
        //计算处理时间
        dataLFU.singleTime = dataLFU.memoryTime * 2 + dataLFU.pageFaultTime;
        console.log("缺页中断发生时间111" + dataLFU.singleTime);
        console.log(dataLFU.position + "位置");
        // 判断内存是否已满
        if (dataLFU.position < dataLFU.pageCount) {
          console.log("页表位置" + dataLFU.position);
          let memoryNumber = dataLFU.position++;
          dataLFU.pageTable.entries[pageNumber] = memoryNumber;
          dataLFU.pageTable.state[pageNumber] = true;
          console.log("内存块号11111：" + memoryNumber);
          // 更新内存
          dataLFU.memory[memoryNumber] = pageNumber;
          return memoryNumber;
        } else {
          // 内存已满，需要进行页面置换
          let minAccessCount = Infinity;
          let replacePageNumber = 0;
          // 遍历页面表，找到访问计数最小的页面
          for (let i = 0; i < dataLFU.pageCount; i++) {
            if (dataLFU.accessCount[dataLFU.memory[i]] < minAccessCount) {
              minAccessCount = dataLFU.accessCount[dataLFU.memory[i]];
              replacePageNumber = dataLFU.memory[i];
            }
          }
          //更细页表
          dataLFU.pageTable.entries[pageNumber] = dataLFU.pageTable.entries[replacePageNumber];
          dataLFU.pageTable.state[replacePageNumber] = false;
          dataLFU.pageTable.state[pageNumber] = true;
          //更新内存
          dataLFU.memory[dataLFU.pageTable.entries[replacePageNumber]] = pageNumber;
          return dataLFU.pageTable.entries[replacePageNumber];
        }
      }
    } else {
      // 使用快表的情况下
      // 判断快表是否命中
      if (dataLFU.fastTable.state[pageNumber]) {
        dataLFU.singleTime = dataLFU.memoryTime + dataLFU.fastAccessTime;
        dataLFU.singlePageFault = false;
        dataLFU.accessCount[pageNumber]++;
        return dataLFU.fastTable.entries[pageNumber];
      } else {
        //快表未命中，需要访问页表
        //页表中存在该页面
        if (dataLFU.pageTable.state[pageNumber]) {
          // 更新访问时间
          // 快表页表一起访问
          if (dataLFU.useTogetherPd) {
            dataLFU.singleTime = dataLFU.time * 2;
          } else {
            dataLFU.singleTime = dataLFU.time * 2 + dataLFU.fastAccessTime;
          }
          dataLFU.singlePageFault = false;
          dataLFU.accessCount[pageNumber]++;
        } else {
          // 页表中不存在该页面
          // 需要进行页面置换
          // 表示缺页
          dataLFU.singlePageFault = true;
          // 缺页个数增加
          dataLFU.pageFaultCount++;
          // 计算处理时间
          if (dataLFU.useTogetherPd) {
            dataLFU.singleTime = dataLFU.memoryTime * 2 + dataLFU.pageFaultTime;
          } else {
            dataLFU.singleTime = dataLFU.memoryTime * 2 + dataLFU.pageFaultTime + dataLFU.fastAccessTime;
          }
          dataLFU.accessCount[pageNumber] = 1;
          // 判断内存是否已满
          if (dataLFU.position < dataLFU.pageCount) {
            let memoryNumber = dataLFU.position++;
            dataLFU.pageTable.entries[pageNumber] = memoryNumber;
            dataLFU.pageTable.state[pageNumber] = true;
            dataLFU.fastTable.entries[pageNumber] = memoryNumber;
            dataLFU.fastTable.state[pageNumber] = true;
            // 更新内存
            dataLFU.memory[memoryNumber] = pageNumber;
            return memoryNumber;
          } else {
            // 内存已满，需要进行页面置换
            let minAccessCount = Infinity;
            let replacePageNumber = 0;
            // 遍历页面表，找到访问计数最小的页面
            for (let i = 0; i < dataLFU.pageCount; i++) {
              if (dataLFU.accessCount[dataLFU.memory[i]] < minAccessCount) {
                minAccessCount = dataLFU.accessCount[dataLFU.memory[i]];
                replacePageNumber = dataLFU.memory[i];
              }
            }
            //更细页表
            dataLFU.pageTable.entries[pageNumber] = dataLFU.pageTable.entries[replacePageNumber];
            dataLFU.pageTable.state[replacePageNumber] = false;
            dataLFU.pageTable.state[pageNumber] = true;
            // 更新快表
            dataLFU.fastTable.entries[pageNumber] = dataLFU.pageTable.entries[replacePageNumber];
            dataLFU.fastTable.state[replacePageNumber] = false;
            dataLFU.fastTable.state[pageNumber] = true;
            //更新内存
            dataLFU.memory[dataLFU.pageTable.entries[replacePageNumber]] = pageNumber;
            return dataLFU.pageTable.entries[replacePageNumber];
          }
        }
      }
    }
  },
  dealWithOPT(pageNumber) {
    if (!dataOPT.useFastPd) {
      // 没有快表
      return methods.dealWithOPTNoFastPd(pageNumber);
    } else {
      // 有快表
      return methods.dealWithOPTWithFastPd(pageNumber);
    }
  },
  dealWithOPTNoFastPd(pageNumber) {
    // 判断页面是否在内存中
    let pageState = dataOPT.pageTable.state[pageNumber];
    console.log("pageState", pageState);
    // 页面在内存中
    if (pageState) {
      dataOPT.singleTime = dataOPT.memoryTime * 2;
      dataOPT.singlePageFault = false;
      let memoryNumber = dataOPT.pageTable.entries[pageNumber];
      // 将该内存块所属页面的下一次出现的索引位置设为最大值
      dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
      // 更新该内存块所属页面的下一次出现的索引位置
      for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
        if (dataOPT.pageQueue[i] === pageNumber) {
          dataOPT.nextIndex[memoryNumber] = i;
          break;
        }
      }
      return dataOPT.pageTable.entries[pageNumber];
    } else {
      dataOPT.singlePageFault = true;
      console.log("页面不在内存中，发生缺页中断");
      // 页面不在内存中，缺页中断
      dataOPT.singleTime = dataOPT.memoryTime * 2 + dataOPT.pageFaultTime;
      if (dataOPT.position < dataOPT.pageCount) {
        // 获取物理块号
        let memoryNumber = dataOPT.position++;
        dataOPT.pageTable.entries[pageNumber] = memoryNumber;
        dataOPT.pageTable.state[pageNumber] = true;
        // 把页面放入内存
        dataOPT.memory[memoryNumber] = pageNumber;
        // 将该内存块所属页面的下一次出现的索引位置设为最大值
        dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
        // 更新该内存块所属页面的下一次出现的索引位置
        for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
          if (dataOPT.pageQueue[i] === pageNumber) {
            dataOPT.nextIndex[memoryNumber] = i;
            break;
          }
        }
        // 返回物理块号
        return memoryNumber;
      } else {
        // 内存已满，选取页面进行置换
        let replacePageNumber;
        let memoryNumber;
        let minNextIndex = -1;
        for (let i = 0; i < dataOPT.pageCount; i++) {
          if (dataOPT.nextIndex[i] > minNextIndex) {
            minNextIndex = dataOPT.nextIndex[i];
            replacePageNumber = dataOPT.memory[i];
            memoryNumber = i;
          }
        }
        // 更新页表
        dataOPT.pageTable.entries[pageNumber] = memoryNumber;
        dataOPT.pageTable.state[pageNumber] = true;
        dataOPT.pageTable.state[replacePageNumber] = false;
        // 更新内存
        dataOPT.memory[memoryNumber] = pageNumber;
        // 将该内存块所属页面的下一次出现的索引位置设为最大值
        dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
        // 更新该内存块所属页面的下一次出现的索引位置
        for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
          if (dataOPT.pageQueue[i] === pageNumber) {
            dataOPT.nextIndex[memoryNumber] = i;
            break;
          }
        }
        // 返回物理块号
        return memoryNumber;
      }
    }
  },
  dealWithOPTWithFastPd(pageNumber) {
    // 判断页面是否在快表中
    if (dataOPT.fastTable.state[pageNumber]) {
      // 快表中存在该页面
      // 更新访问时间
      dataOPT.singleTime = dataOPT.fastAccessTime + dataOPT.memoryTime;
      dataOPT.singlePageFault = false;
      let memoryNumber = dataOPT.fastTable.entries[pageNumber];
      // 将该内存块所属页面的下一次出现的索引位置设为最大值
      dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
      // 更新该内存块所属页面的下一次出现的索引位置
      for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
        if (dataOPT.pageQueue[i] === pageNumber) {
          dataOPT.nextIndex[memoryNumber] = i;
          break;
        }
      }
      return dataOPT.fastTable.entries[pageNumber];
    } else {
      // 快表中不存在该页面
      // 判断页表中是否存在该页面
      if (dataOPT.pageTable.state[pageNumber]) {
        // 页表中存在该页面
        // 更新访问时间
        if (dataOPT.useTogetherPd) {
          dataOPT.singleTime = dataOPT.memoryTime * 2;
        } else {
          dataOPT.singleTime = dataOPT.memoryTime * 2 + dataOPT.fastAccessTime;
        }
        dataOPT.singlePageFault = false;
        let memoryNumber = dataOPT.pageTable.entries[pageNumber];
        // 将该内存块所属页面的下一次出现的索引位置设为最大值
        dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
        // 更新该内存块所属页面的下一次出现的索引位置
        for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
          if (dataOPT.pageQueue[i] === pageNumber) {
            dataOPT.nextIndex[memoryNumber] = i;
            break;
          }
        }
        return dataOPT.pageTable.entries[pageNumber];
      } else {
        // 页表中不存在该页面
        if (dataOPT.useTogetherPd) {
          dataOPT.singleTime = dataOPT.pageFaultTime + dataOPT.memoryTime * 2;
        } else {
          dataOPT.singleTime = dataOPT.pageFaultTime + dataOPT.memoryTime * 2 + dataOPT.fastAccessTime;
        }
        dataOPT.singlePageFault = true;
        // 判断内存是否已满
        if (dataOPT.position < dataOPT.pageCount) {
          // 获取物理块号
          let memoryNumber = dataOPT.position++;
          dataOPT.pageTable.entries[pageNumber] = memoryNumber;
          dataOPT.pageTable.state[pageNumber] = true;
          dataOPT.fastTable.entries[pageNumber] = memoryNumber;
          dataOPT.fastTable.state[pageNumber] = true;
          // 把页面放入内存
          dataOPT.memory[memoryNumber] = pageNumber;
          // 将该内存块所属页面的下一次出现的索引位置设为最大值
          dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
          // 更新该内存块所属页面的下一次出现的索引位置
          for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
            if (dataOPT.pageQueue[i] === pageNumber) {
              dataOPT.nextIndex[memoryNumber] = i;
              break;
            }
          }
          // 返回物理块号
          return memoryNumber;
        } else {
          // 内存已满，选取页面进行置换
          let replacePageNumber;
          let memoryNumber;
          let minNextIndex = -1;
          for (let i = 0; i < dataOPT.pageCount; i++) {
            if (dataOPT.nextIndex[i] > minNextIndex) {
              minNextIndex = dataOPT.nextIndex[i];
              replacePageNumber = dataOPT.memory[i];
              memoryNumber = i;
            }
          }
          // 更新页表
          dataOPT.pageTable.entries[pageNumber] = memoryNumber;
          dataOPT.pageTable.state[pageNumber] = true;
          dataOPT.pageTable.state[replacePageNumber] = false;
          // 更新快表
          dataOPT.fastTable.entries[pageNumber] = memoryNumber;
          dataOPT.fastTable.state[pageNumber] = true;
          dataOPT.fastTable.state[replacePageNumber] = false;
          // 更新内存
          dataOPT.memory[memoryNumber] = pageNumber;
          // 将该内存块所属页面的下一次出现的索引位置设为最大值
          dataOPT.nextIndex[memoryNumber] = dataOPT.pageQueue.length;
          // 更新该内存块所属页面的下一次出现的索引位置
          for (let i = dataOPT.index; i < dataOPT.pageQueue.length; i++) {
            if (dataOPT.pageQueue[i] === pageNumber) {
              dataOPT.nextIndex[memoryNumber] = i;
              break;
            }
          }
          // 返回物理块号
          return memoryNumber;
        }
      }
    }
  },
  async begin() {
    //处理逻辑地址序列组
    console.log("进入begin");
    methods.strsToStr();
    //tableData.value = [];
    //定义页表
    data.pageTable = {
      //存取页表项的数组
      //数组entries的每个下标代表页号
      //每个下标对应的值是物理块号
      entries: [],
      //页表项状态：代表某页是否在内存中
      state: []
    };
    //定义快表
    data.fastTable = {
      entries: [],
      state: []
    };
    data.time = parseInt(formData.value.time);
    data.pause = parseInt(formData.value.pause);
    data.fastTime = parseInt(formData.value.fastform);
    result.algorithm.addressLength = data.logicalAddresses.length;
    //逐个遍历逻辑地址
    //逻辑地址：pageNumber + offset
    for (let i = 0; i < data.logicalAddresses.length; i++) {
      data1.value.twoDArray[0][0] = -1;
      data.timeFIFO = 0;
      data.memorySequences = [];
      //通过逻辑地址获取页号
      let pageNumber = methods.getPageNumber(data.logicalAddresses[i]);
      console.log("pageNumber" + pageNumber);
      //将data.logicalAddresses[i]转换成十进制
      let pageNumberDecimal = parseInt(data.logicalAddresses[i], 16);
      //获取页内偏移量
      let offset = pageNumberDecimal % (pageNumber * 4096);
      if (pageNumber === 0) {
        offset = pageNumberDecimal;
      }
      //获取物理地址(采用FIFO页面置换算法)
      console.log("offset" + offset);
      let physicalAddress = methods.dealWithFIFO(pageNumber) * 4096 + offset;
      result.algorithmFIFO.time[i] = data.timeFIFO;
      if (i !== 0) {
        await methods.sleep(data.timeFIFO);
      }
      //输出物理地址
      console.log("物理地址" + physicalAddress);
      console.log("页号队列" + data.memory);
      console.log(formData.value.num + 1, i + 1);
      if (data.shortPage === 1) {
        // console.log(formData.value.num + 1, i + 1);
        data1.value.twoDArray[formData.value.num + 1][i + 1] = "√";
        data1.value.pageFault++;
      } else {
        data1.value.twoDArray[formData.value.num + 1][i + 1] = "×";
      }
      data.shortPageCount[i] = data1.value.pageFault;
      console.log("缺页次数sssq  " + data.shortPageCount[i]);
      data1.value.pageFaultRate[i] = data1.value.pageFault / (i + 1);
      console.log("缺页率ddd   " + data1.value.pageFaultRate[i]);
      data1.value.twoDArray[formData.value.num + 1][0] = "缺页";
      data1.value.twoDArray[formData.value.num + 2][0] = "物理地址";
      data1.value.twoDArray[formData.value.num + 3][0] = "执行时间";
      let truephysicalAddress = physicalAddress.toString(16).toUpperCase();
      if (truephysicalAddress.length === 3) {
        truephysicalAddress = "0" + truephysicalAddress;
      }
      result.algorithmFIFO.physicalAddress.push(truephysicalAddress);
      data1.value.twoDArray[formData.value.num + 2][i + 1] = truephysicalAddress;
      data1.value.twoDArray[formData.value.num + 3][i + 1] = result.algorithmFIFO.time[i];
      let count = 0;
      for (let k = 1; k <= data.logicalAddresses.length; k++) {
        data1.value.twoDArray[0][k] = data.logicalAddresses[k - 1];
      }
      for (let j = 1; j <= formData.value.num; j++) {
        data1.value.twoDArray[j][0] = count;
        count++;
        data1.value.twoDArray[j][i + 1] = data.memory[j - 1];
        console.log(data1.value.twoDArray[j][i], "datxxx");
        // 延迟两秒进入下一循环
      }
      while (data.stop) {
        await methods.sleep();
      }
      updateChart();
      updataChart2();
      percentageCount.value++;
      percentageResult.value = parseFloat(((percentageCount.value * 100) / (data.strsArray.length * 4)).toFixed(2));
      console.log(percentageResult.value, "执行率");
    }
    result.algorithmFIFO.pageFaultRate = data1.value.pageFaultRate[data.logicalAddresses.length - 1];
    result.algorithmFIFO.pageFault = data1.value.pageFault;
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(data * 2);
      }, 1000);
    });
  },
  async begin1() {
    console.log("进入begin1");
    dataLRU.memoryTime = parseInt(formData.value.time);
    dataLRU.fastAccessTime = parseInt(formData.value.fastform);
    dataLRU.pageFaultTime = parseInt(formData.value.pause);
    dataLRU.fastTablePd = formData.value.form;
    dataLRU.useTogetherPd = formData.value.together;
    methods.strsToStrLRU();
    //逐个遍历逻辑地址
    //逻辑地址：pageNumber + offset
    //定义LRU算法中的哈希表
    data.pageTableLRU = new Map();
    console.log("qweqweqwewe");
    //定义LRU算法中的快表
    data.fastTableLRU = new Map();
    console.log("123qqq");
    //逐个遍历逻辑地址
    //逻辑地址：pageNumber + offset
    for (let i = 0; i < data.logicalAddressesLRU.length; i++) {
      data1.value.twoDArray1[0][0] = -1;
      data.timeFIFO = 0;
      data.memorySequences = [];
      //通过逻辑地址获取页号
      let pageNumber = methods.getPageNumber(data.logicalAddressesLRU[i]);
      console.log("pageNumberLRU" + pageNumber);
      //将data.logicalAddresses[i]转换成十进制
      let pageNumberDecimal = parseInt(data.logicalAddressesLRU[i], 16);
      //获取页内偏移量
      let offset = pageNumberDecimal % (pageNumber * 4096);
      if (pageNumber === 0) {
        offset = pageNumberDecimal;
      }
      //获取物理地址(采用LRU页面置换算法)
      console.log("offsetLRU" + offset);
      let physicalAddress = methods.dealWithLRU(pageNumber) * 4096 + offset;
      dataLRU.time[i] = dataLRU.singleTime;
      result.algorithmLRU.time.push(dataLRU.time[i]);
      if (i !== 0) {
        await methods.sleep(dataLRU.singleTime);
      }
      //输出物理地址
      console.log("LRU  物理地址" + physicalAddress);
      console.log("LRU  页号队列" + data.memoryLRU);
      console.log(formData.value.num + 1, i + 1);
      if (data.shortPageLRU === 1) {
        data1.value.twoDArray1[formData.value.num + 1][i + 1] = "√";
        data1.value.pageFaultLRU++;
      } else {
        data1.value.twoDArray1[formData.value.num + 1][i + 1] = "×";
      }
      data.shortPageCountLRU[i] = data1.value.pageFaultLRU;
      console.log("LRU缺页次数" + data.shortPageCountLRU[i]);
      data1.value.pageFaultRateLRU[i] = data1.value.pageFaultLRU / (i + 1);
      console.log("缺页率LRU" + data1.value.pageFaultRateLRU[i]);
      data1.value.twoDArray1[formData.value.num + 1][0] = "缺页";
      data1.value.twoDArray1[formData.value.num + 2][0] = "物理地址";
      data1.value.twoDArray1[formData.value.num + 3][0] = "执行时间";
      let truephysicalAddress = physicalAddress.toString(16).toUpperCase();
      if (truephysicalAddress.length === 3) {
        truephysicalAddress = "0" + truephysicalAddress;
      }
      result.algorithmLRU.physicalAddress.push(truephysicalAddress);
      data1.value.twoDArray1[formData.value.num + 2][i + 1] = truephysicalAddress;
      data1.value.twoDArray1[formData.value.num + 3][i + 1] = dataLRU.time[i];
      let count = 0;
      for (let k = 1; k <= data.logicalAddressesLRU.length; k++) {
        data1.value.twoDArray1[0][k] = data.logicalAddressesLRU[k - 1];
      }
      for (let j = 1; j <= formData.value.num; j++) {
        data1.value.twoDArray1[j][0] = count;
        count++;
        data1.value.twoDArray1[j][i + 1] = data.memoryLRU[j - 1];
        console.log(data1.value.twoDArray1[j][i], "datxxxx");
        // 延迟两秒进入下一循环
      }
      while (data.stop) {
        await methods.sleep();
      }
      updateChart();
      updataChart2();
      percentageCount.value++;
      percentageResult.value = parseFloat(((percentageCount.value * 100) / (data.strsArray.length * 4)).toFixed(2));
    }
    result.algorithmLRU.pageFaultRate = data1.value.pageFaultRateLRU[data.logicalAddressesLRU.length - 1];
    result.algorithmLRU.pageFault = data1.value.pageFaultLRU;
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(data * 3);
      }, 1500);
    });
  },
  async beginWithLFU() {
    dataLFU.strs = formData.value.data;
    dataLFU.memoryTime = parseInt(formData.value.time);
    dataLFU.pageFaultTime = parseInt(formData.value.pause);
    dataLFU.fastAccessTime = parseInt(formData.value.fastform);
    dataLFU.pageCount = parseInt(formData.value.num);
    dataLFU.useFastPd = formData.value.form;
    dataLFU.useTogetherPd = formData.value.together;
    dataLFU.position = 0;
    methods.strsToStrLFU();
    for (let i = 0; i < dataLFU.logicalAddresses.length; i++) {
      data1.value.twoDArray2[0][0] = -1;
      let pageNumber = methods.getPageNumberLFU(dataLFU.logicalAddresses[i]);
      console.log("pageNumberLFU", pageNumber);
      let pageNumberDecimal = parseInt(dataLFU.logicalAddresses[i], 16);
      let offset = pageNumberDecimal % (pageNumber * 4096);
      if (pageNumber === 0) {
        offset = pageNumberDecimal;
      }
      console.log("offsetLFU1", offset);
      let physicalAddressLFU = methods.dealWithLFU(pageNumber) * 4096 + offset;
      console.log("物理地址LFU1：" + physicalAddressLFU);
      dataLFU.time[i] = dataLFU.singleTime;
      result.algorithmLFU.time.push(dataLFU.time[i]);
      dataLFU.pageFault[i] = dataLFU.singlePageFault;
      if (i !== 0) {
        await methods.sleep(dataLFU.time[i]);
      }
      console.log("逻辑地址处理时间:" + dataLFU.time[i]);
      console.log("缺页：" + dataLFU.pageFault[i]);
      console.log("物理块号队列：" + dataLFU.memory);
      data1.value.twoDArray2[formData.value.num + 1][0] = "缺页";
      data1.value.twoDArray2[formData.value.num + 2][0] = "物理地址";
      data1.value.twoDArray2[formData.value.num + 3][0] = "执行时间";
      let truephysicalAddressLFU = physicalAddressLFU.toString(16).toUpperCase();
      if (truephysicalAddressLFU.length === 3) {
        truephysicalAddressLFU = "0" + truephysicalAddressLFU;
      }
      result.algorithmLFU.physicalAddress.push(truephysicalAddressLFU);
      data1.value.twoDArray2[formData.value.num + 2][i + 1] = truephysicalAddressLFU;
      data1.value.twoDArray2[formData.value.num + 3][i + 1] = dataLFU.time[i];
      if (dataLFU.pageFault[i] === true) {
        data1.value.twoDArray2[formData.value.num + 1][i + 1] = "√";
        data1.value.pageFaultLFU++;
      } else {
        data1.value.twoDArray2[formData.value.num + 1][i + 1] = "×";
      }
      data.shortPageCountLFU[i] = data1.value.pageFaultLFU;
      data1.value.pageFaultRateLFU[i] = data1.value.pageFaultLFU / (i + 1);
      console.log("缺页率LFU" + data1.value.pageFaultRateLFU[i]);
      let count = 0;
      for (let k = 1; k <= dataLFU.logicalAddresses.length; k++) {
        data1.value.twoDArray2[0][k] = dataLFU.logicalAddresses[k - 1];
      }
      for (let j = 1; j <= formData.value.num; j++) {
        data1.value.twoDArray2[j][0] = count;
        count++;
        data1.value.twoDArray2[j][i + 1] = dataLFU.memory[j - 1];
      }
      while (data.stop) {
        await methods.sleep();
      }
      updateChart();
      updataChart2();
      percentageCount.value++;
      percentageResult.value = parseFloat(((percentageCount.value * 100) / (data.strsArray.length * 4)).toFixed(2));
    }
    result.algorithmLFU.pageFaultRate = data1.value.pageFaultRateLFU[data.logicalAddresses.length - 1];
    result.algorithmLFU.pageFault = data1.value.pageFaultLFU;
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(data * 2);
      }, 500);
    });
  },
  async beginWithOPT() {
    dataOPT.strs = formData.value.data;
    dataOPT.memoryTime = parseInt(formData.value.time, 10);
    dataOPT.pageFaultTime = parseInt(formData.value.pause, 10);
    dataOPT.fastAccessTime = parseInt(formData.value.fastform, 10);
    dataOPT.pageCount = parseInt(formData.value.num, 10);
    dataOPT.useFastPd = formData.value.form;
    dataOPT.useTogetherPd = formData.value.together;
    dataOPT.index = 0;
    dataOPT.position = 0;
    console.log("dataOPT");
    methods.strsToStrOPT();
    console.log(dataOPT.logicalAddresses);
    dataOPT.pageQueueLength = dataOPT.logicalAddresses.length;
    console.log("pageQueueLength", dataOPT.pageQueueLength);
    for (let i = 0; i < dataOPT.logicalAddresses.length; i++) {
      let pageNumber = methods.getPageNumberOPT(dataOPT.logicalAddresses[i]);
      dataOPT.pageQueue[i] = pageNumber;
    }
    console.log("offsetQueue", dataOPT.offsetQueue);
    console.log("pageQueue", dataOPT.pageQueue);
    for (let i = 0; i < dataOPT.logicalAddresses.length; i++) {
      dataOPT.index = i + 1;
      let pageNumber = dataOPT.pageQueue[i];
      console.log("pageNumber", pageNumber);
      let pageNumberDecimal = parseInt(dataOPT.logicalAddresses[i], 16);
      console.log("pageNumberDecimal", pageNumberDecimal);
      let pageNumberInt = parseInt(pageNumber, 16);
      console.log("pageNumberInt", pageNumberInt);
      let offset = pageNumberDecimal % (pageNumberInt * 4096);
      if (pageNumberInt === 0) {
        offset = pageNumberDecimal;
      }
      let physicalAddress = methods.dealWithOPT(pageNumber) * 4096 + offset;
      // 输出物理地址
      console.log("物理地址", physicalAddress);
      // 输出处理时间
      console.log(dataOPT.singleTime);
      dataOPT.singleTime = parseInt(dataOPT.singleTime, 10);
      dataOPT.time[i] = dataOPT.singleTime;
      result.algorithmOPT.time.push(dataOPT.time[i]);
      console.log("OPT时间", dataOPT.time[i]);
      // 输出队列
      console.log("内存队列", dataOPT.memory);
      // 输出是否缺页的判断
      console.log(dataOPT.singlePageFault);
      dataOPT.pageFault[i] = dataOPT.singlePageFault;
      if (i !== 0) {
        await methods.sleep(dataOPT.singleTime);
      }
      data1.value.twoDArray3[formData.value.num + 1][0] = "缺页";
      data1.value.twoDArray3[formData.value.num + 2][0] = "物理地址";
      data1.value.twoDArray3[formData.value.num + 3][0] = "执行时间";
      let truephysicalAddressOPT = physicalAddress.toString(16).toUpperCase();
      if (truephysicalAddressOPT.length === 3) {
        truephysicalAddressOPT = "0" + truephysicalAddressOPT;
      }
      result.algorithmOPT.physicalAddress.push(truephysicalAddressOPT);
      data1.value.twoDArray3[formData.value.num + 2][i + 1] = truephysicalAddressOPT;
      data1.value.twoDArray3[formData.value.num + 3][i + 1] = dataOPT.time[i];
      if (dataOPT.pageFault[i] === true) {
        data1.value.twoDArray3[formData.value.num + 1][i + 1] = "√";
        data1.value.pageFaultOPT++;
      } else {
        data1.value.twoDArray3[formData.value.num + 1][i + 1] = "×";
      }
      data.shortPageCountOPT[i] = data1.value.pageFaultOPT;
      data1.value.pageFaultRateOPT[i] = data1.value.pageFaultOPT / (i + 1);
      console.log("缺页率OPT" + data1.value.pageFaultRateOPT[i]);
      let count = 0;
      for (let k = 1; k <= dataOPT.logicalAddresses.length; k++) {
        data1.value.twoDArray3[0][k] = dataOPT.logicalAddresses[k - 1];
      }
      for (let j = 1; j <= formData.value.num; j++) {
        data1.value.twoDArray3[j][0] = count;
        count++;
        data1.value.twoDArray3[j][i + 1] = dataOPT.memory[j - 1];
      }
      while (data.stop) {
        await methods.sleep();
      }
      updateChart();
      updataChart2();
      percentageCount.value++;
      percentageResult.value = parseFloat(((percentageCount.value * 100) / (data.strsArray.length * 4)).toFixed(2));
    }
    result.algorithmOPT.pageFaultRate = data1.value.pageFaultRateOPT[data.logicalAddresses.length - 1];
    result.algorithmOPT.pageFault = data1.value.pageFaultOPT;
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(data * 2);
      }, 500);
    });
  },
  getPageNumber(address) {
    let addressInt = parseInt(address, 16);
    let pageSize = 4096;
    let pageNumber = Math.floor(addressInt / pageSize);
    console.log(`逻辑地址 ${address} 对应的页号为 ${pageNumber}`);
    return pageNumber;
  },
  getPageNumberLFU(address) {
    let addressInt = parseInt(address, 16);
    let pageSize = 4096;
    let pageNumber = Math.floor(addressInt / pageSize);
    console.log(`逻辑地址 ${addressInt} 对应的页号为 ${pageNumber}`, "qweqweqweqe");
    return pageNumber;
  },
  getPageNumberOPT(address) {
    let addressInt = parseInt(address, 16);
    let pageSize = 4096;
    let pageNumber = Math.floor(addressInt / pageSize);
    return pageNumber;
  },
  //更新LRU算法中的访问循序的函数
  updateState(pageNumber) {
    // 将访问的页面移至访问顺序的末尾，表示最近访问
    data.pageQueueLRU = data.pageQueueLRU.filter(item => item !== pageNumber);
    //此处filter函数的作用:
    // 移除数组中所有与给定函数返回值为 true 的元素，并返回移除元素后的新数组。
    data.pageQueueLRU.push(pageNumber);
  },
  async runAsyncMethods() {
    await Promise.all([methods.begin(), methods.begin1(), methods.beginWithLFU(), methods.beginWithOPT()]);
  },
  save() {
    // 将数据转换为JSON字符串
    let json = JSON.stringify(result, null, 2); // 使用缩进格式美化输出

    // 创建Blob对象
    let blob = new Blob([json], { type: "application/json" });

    // 创建下载链接
    let url = URL.createObjectURL(blob);
    let link = document.createElement("a");
    link.href = url;
    link.download = "data.json"; // 你想要的文件名

    // 触发下载
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);

    // 释放URL对象
    URL.revokeObjectURL(url);
  }
};
</script>
<style scoped lang="scss">
@import "./index.scss";
.table-container {
  display: flex;
}

.custom-table {
  border: 2px solid black;
  margin: 10px;
}

.custom-table td {
  border: 2px solid #afafaf;
  width: 70px;
}

.custom-table {
  border: 2px solid #eeeeee;
  text-align: center; /* 文字居中 */
  line-height: 30px; /* 垂直居中 */
  margin-left: 30px; /* 设置单元格之间的距离 */
  margin-bottom: 30px;
}

.cell {
  border: 10px;
  padding: 0px;
}
.cellSet {
  width: 70px;
}
.demo-progress .el-progress--line {
  margin-top: 30px;
  margin-left: 30px;
  margin-bottom: 15px;
  width: 65%;
}
</style>
